
//获取应用实例
const app = getApp()

Component({
  data: {
    showTopTips: false,
    memberInfo: null,
    wh:0,
    formData: {

    },
    education: ["博士研究生", "硕士研究生", "大学", "大专", "高中", "中专", "初中"],
    educationIndex: 0,
    healthStatus: ["健康或良好", "一般或较弱有病", "有生理缺陷残疾"],
    healthStatusIndex: 0,
    marryStatus: ["未婚", "已婚", "丧偶", "离婚", "再婚", "其他"],
    marryStatusIndex: 0,
    nation: ["汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族", "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族", "其他", "外国血统", "外国民族"
],
    nationIndex: 0,
    sex: ["男", "女"],
    sexIndex: 0,
    rules: [{
      name: 'name',
      rules: [{ required: true, message: '请输入姓名' }],
    }, {
      name: 'idCard',
      rules: {
        validator: function (rule, value, param, modeels) {
          if (!value || value.length !== 18) {
            return 'idcard格式不正确'
          }
        }
      },
    }, {
      name: 'birthday',
      rules: [{ required: true, message: '请输入出生日期' }],
    }, {
    name: 'addr',
    rules: [{ required: true, message: '请输入家庭住址' }],
    }, {
      name: 'email',
      rules: [{ required: true, message: '请输入常用邮箱' }, { email: true, message: '邮箱格式不对' }],
    }, {
      name: 'friendTel',
      rules: [{ required: true, message: '请输入亲属手机号' }, { mobile: true, message: '亲属手机号格式不对' }],
    }, {
      name: 'registerAddr',
      rules: [{ required: true, message: '请输入户口所在' }]
    }]
  },
  methods: {
    onShow(){
      var that = this;
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
            })
          }
        })
      },
    goBack: function(){
      wx.navigateBack({
        delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
      });
    },
    bindMarryStatusChange: function (e) {
      this.setData({
        marryStatusIndex: e.detail.value
      })
    },
    bindHealthStatusChange: function (e) {
      this.setData({
        healthStatusIndex: e.detail.value
      })
    },
    bindEducationChange: function (e) {
      this.setData({
        educationIndex: e.detail.value
      })
    },
    bindSexChange: function (e) {
      this.setData({
        sexIndex: e.detail.value
      })
    },
    bindNationChange: function (e) {
      this.setData({
        nationIndex: e.detail.value
      })
    }, 
    bindBirthdayChange: function (e) {
      this.setData({
        birthday: e.detail.value,
        [`formData.birthday`]: e.detail.value
      })
    },
    formInputChange(e) {
      const { field } = e.currentTarget.dataset
      this.setData({
        [`formData.${field}`]: e.detail.value
      })
    },
    onLoad: function(){
      if (app.globalData.memberInfo) {
        this.setData({
          memberInfo: app.globalData.memberInfo,
          educationIndex: this.data.education.indexOf(app.globalData.memberInfo.education),
          healthStatusIndex: this.data.healthStatus.indexOf(app.globalData.memberInfo.healthStatus),
          marryStatusIndex: this.data.marryStatus.indexOf(app.globalData.memberInfo.marryStatus),
          sexIndex: app.globalData.memberInfo.sex,
          nationIndex: this.data.nation.indexOf(app.globalData.memberInfo.nation),
          birthday: app.globalData.memberInfo.birthdayStr,
          [`formData.name`]: app.globalData.memberInfo.memberName,
          [`formData.idCard`]: app.globalData.memberInfo.idCard,
          [`formData.sex`]: app.globalData.memberInfo.sex,
          [`formData.nation`]: app.globalData.memberInfo.nation,
          [`formData.birthday`]: app.globalData.memberInfo.birthdayStr,
          [`formData.addr`]: app.globalData.memberInfo.addr,
          [`formData.email`]: app.globalData.memberInfo.email,
          [`formData.friendTel`]: app.globalData.memberInfo.friendTel,
          [`formData.registerAddr`]: app.globalData.memberInfo.registerAddr,
          [`formData.marryStatus`]: app.globalData.memberInfo.marryStatus,
          [`formData.healthStatus`]: app.globalData.memberInfo.healthStatus,
          [`formData.education`]: app.globalData.memberInfo.education
        })
      }
      else {
        app.memberInfoReadyCallback = res => {
          this.setData({
            memberInfo: app.globalData.memberInfo,
            educationIndex: this.data.education.indexOf(app.globalData.memberInfo.education),
            healthStatusIndex: this.data.healthStatus.indexOf(app.globalData.memberInfo.healthStatus),
            marryStatusIndex: this.data.marryStatus.indexOf(app.globalData.memberInfo.marryStatus),
            sexIndex: this.data.sex.indexOf(app.globalData.memberInfo.sex),
            nationIndex: this.data.nation.indexOf(app.globalData.memberInfo.nation),
            birthday: app.globalData.memberInfo.birthdayStr,
            [`formData.name`]: app.globalData.memberInfo.memberName,
            [`formData.idCard`]: app.globalData.memberInfo.idCard,
            [`formData.sex`]: app.globalData.memberInfo.sex,
            [`formData.nation`]: app.globalData.memberInfo.nation,
            [`formData.birthday`]: app.globalData.memberInfo.birthdayStr,
            [`formData.addr`]: app.globalData.memberInfo.addr,
            [`formData.email`]: app.globalData.memberInfo.email,
            [`formData.friendTel`]: app.globalData.memberInfo.friendTel,
            [`formData.registerAddr`]: app.globalData.memberInfo.registerAddr,
            [`formData.marryStatus`]: app.globalData.memberInfo.marryStatus,
            [`formData.healthStatus`]: app.globalData.memberInfo.healthStatus,
            [`formData.education`]: app.globalData.memberInfo.education
          })
        }
      }
    },
    submitForm() {
        this.selectComponent('#form').validate((valid, errors) => {
            console.log('valid', valid, errors)
            if (!valid) {
                const firstError = Object.keys(errors)
                if (firstError.length) {
                    this.setData({
                        error: errors[firstError[0]].message
                    })

                }
            } else {
              wx.request({
                url: 'https://www.xinhuo.site/api/partyMember/edit',
                data: {
                  id: this.data.memberInfo.id,
                  memberName: this.data.formData.name,
                  idCard: this.data.formData.idCard,
                  sex: this.data.sexIndex,
                  nation: this.data.nation[this.data.nationIndex],
                  birthday: this.data.formData.birthday,
                  addr: this.data.formData.addr,
                  email: this.data.formData.email,
                  friendTel: this.data.formData.friendTel,
                  registerAddr: this.data.formData.registerAddr,
                  marryStatus: this.data.marryStatus[this.data.marryStatusIndex],
                  healthStatus: this.data.healthStatus[this.data.healthStatusIndex],
                  education: this.data.education[this.data.educationIndex]
                },
                method: 'POST',
                header: {
                  'Content-Type': 'application/json'
                },
                success(res) {
                  if (res.data.syscode == 0) {
                    app.globalData.memberInfo = res.data.memberInfo;
                    wx.showToast({
                      title: '校验通过'
                    });
                    wx.navigateBack({
                      delta: 1
                    });
                  }
                  else if (res.data.syscode == 1) {
                    wx.showToast({
                      title: '无法匹配多名用户',
                      icon: 'none'
                    });
                  }
                  else if (res.data.syscode == 2) {
                    wx.showToast({
                      title: '没有匹配的用户',
                      icon: 'none'
                    });
                  }
                  else {
                    wx.showToast({
                      title: '操作失败请联系管理员',
                      icon: 'none'
                    });
                  }
                }
              })
                
            }
        })
        // this.selectComponent('#form').validateField('mobile', (valid, errors) => {
        //     console.log('valid', valid, errors)
        // })
    }

  },
  externalClasses: ["my-page"]
});